package com.ljw.vendingmachine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ljw.vendingmachine.base.BaseActivity;
import com.ljw.vendingmachine.bean.MessageEvent;
import com.ljw.vendingmachine.utils.Constant;
import com.ljw.vendingmachine.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class ConnectSettingActivity extends BaseActivity {
//    @BindView(R.id.layout_password)
//    LinearLayout layout_password;
//    @BindView(R.id.ed_password)
//    EditText ed_password;
//    @BindView(R.id.btn_yes)
//    Button btn_yes;

    @BindView(R.id.layout_settings)
    LinearLayout layout_settings;
    @BindView(R.id.ed_IP)
    EditText ed_IP;
    @BindView(R.id.ed_port)
    EditText ed_port;

    @BindView(R.id.ed_deviceID)
    EditText ed_deviceID;
    @BindView(R.id.btn_Sava)
    Button btn_Sava;

    @Override
    public int setContentView() {
        return R.layout.activity_connect_setting;
    }



    @Override
    public void initView() {
//        if (SPUtils.contains(this, Constant.PASSWORD)){
//            layout_password.setVisibility(View.VISIBLE);
//            layout_settings.setVisibility(View.GONE);
//        }else {
//            layout_password.setVisibility(View.GONE);
//            layout_settings.setVisibility(View.VISIBLE);
//        }
//        btn_yes.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String password = ed_password.getText().toString().trim();
//                if (!TextUtils.isEmpty(password)&&SPUtils.get(ConnectSettingActivity.this,Constant.PASSWORD,"").toString().equals(password)){
//                    layout_password.setVisibility(View.GONE);
//                    layout_settings.setVisibility(View.VISIBLE);
//                }
//            }
//        });

        if (SPUtils.contains(this,Constant.IP)){
            String ip= (String) SPUtils.get(this,Constant.IP,"");
            ed_IP.setText(ip);
        }
        if (SPUtils.contains(this,Constant.PORT)){
            String ip= (String) SPUtils.get(this,Constant.PORT,"");
            ed_port.setText(ip);
        }
        if (SPUtils.contains(this,Constant.DEVICEID)){
            String deviceid= (String) SPUtils.get(this,Constant.DEVICEID,"");
            ed_deviceID.setText(deviceid);
        }
        btn_Sava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ip = ed_IP.getText().toString().trim();
                if (TextUtils.isEmpty(ip)){
                    Toast.makeText(ConnectSettingActivity.this,"IP不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                String port = ed_port.getText().toString().trim();
                if (TextUtils.isEmpty(port)){
                    Toast.makeText(ConnectSettingActivity.this,"端口不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                String id = ed_deviceID.getText().toString().trim();
                if (TextUtils.isEmpty(id)){
                    Toast.makeText(ConnectSettingActivity.this,"ID不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                Constant.BASEURL=ip;
                Constant.BASEPORT=port;
                SPUtils.put(ConnectSettingActivity.this,Constant.IP,Constant.BASEURL);
                SPUtils.put(ConnectSettingActivity.this,Constant.PORT,Constant.BASEPORT);
                SPUtils.put(ConnectSettingActivity.this,Constant.DEVICEID,id);
                Toast.makeText(ConnectSettingActivity.this,"保存成功",Toast.LENGTH_LONG).show();
                startActivity(new Intent(ConnectSettingActivity.this,MainActivity.class));
                MessageEvent mes = new MessageEvent();
                mes.setCode(Constant.CONNECT);
                //       mes.setData(String.format(Constant.COMD_GOODS_OUT_SECCESS, getQRcode(MainActivity.goodsMap), orderNo));
                EventBus.getDefault().post(mes);
                finish();

            }
        });


    }

    @Override
    public void initData() {

    }
}
